package com.car.shopping.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.car.shopping.controller.form.AddPRForm;
import com.car.shopping.entity.ProductReview;
import com.car.shopping.mapper.ProductReviewMapper;
import com.car.shopping.service.ProductReviewService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.car.shopping.service.param.AddPRParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 董旭
 * @since 2024-08-28
 */
@Service
public class ProductReviewServiceImpl extends ServiceImpl<ProductReviewMapper, ProductReview> implements ProductReviewService {
    @Autowired
    private ProductReviewMapper productReviewMapper;
    @Override
    public List<ProductReview> selectPRById(Integer goodsId) {
        // 使用 MyBatis-Plus 提供的 QueryWrapper 构造查询条件
        QueryWrapper<ProductReview> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_id", goodsId);

        // 使用 MyBatis-Plus 的 selectList 方法查询符合条件的商品评价列表
        List<ProductReview> reviews = productReviewMapper.selectList(queryWrapper);
        return reviews;
    }

    @Override
    public void addUserPR(AddPRParam param, Integer userId) throws Exception {
        // 参数校验
        if (param.getGoodsId() == null || param.getUserPhone() == null ) {
            throw new Exception("传的有问题");
        }

        // 查询是否已经评价过
        QueryWrapper<ProductReview> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ProductReview::getGoodsId, param.getGoodsId()) // 按商品ID查询
                .eq(ProductReview::getUserPhone, param.getUserPhone()); // 按用户手机号查询

        // 1. 查询数据库是否有已有评论
        ProductReview existingReview = productReviewMapper.selectOne(queryWrapper);

        if (existingReview != null) {
            // 如果已经存在评价，检查这次传递的评分是否为空，如果不为空则抛出异常
            if (param.getRating() != null) {
                throw new Exception("已经打过分了，不能重复打分");
            }
        } else {
            // 如果没有找到评价，检查评分是否传递
            if (param.getRating() == null) {
                throw new Exception("第一次评论必须传评分");
            }
        }

        // 2. 插入新的评价记录
        ProductReview review = new ProductReview();
        review.setUserId(userId);
        review.setGoodsId(param.getGoodsId());
        review.setUserPhone(param.getUserPhone());
        review.setRating(param.getRating());
        review.setComment(param.getComment());
        review.setParentId(param.getParentId());
        review.setCreatedTime(LocalDateTime.now());
        // 3. 保存到数据库
        productReviewMapper.insert(review);

        // 日志
        System.out.println("评论成功，userId: " + userId + ", goodsId: " + param.getGoodsId());
    }


    @Override
    public void addAdminPR(AddPRParam param, Integer adminId) throws Exception {
        // 参数校验


        ProductReview review = new ProductReview();
        review.setUserId(adminId);
        review.setGoodsId(param.getGoodsId());
        review.setUserPhone(param.getUserPhone());
        review.setComment(param.getComment());
        review.setParentId(param.getParentId());
        // 3. 保存到数据库
        productReviewMapper.insert(review);
    }

    @Override
    public void deleteReviewById(Integer reviewId) throws Exception {
        // 查找评价
        ProductReview review = productReviewMapper.selectById(reviewId);
        if (review == null) {
            throw new Exception("评价不存在");
        }

        // 更新评价内容为“【该条评论已被删除】”
        review.setComment("【该条评论已被删除】");

        // 保存更新
        productReviewMapper.updateById(review);
    }

    @Override
    public List<ProductReview> allReview() throws Exception {
        // 使用 MyBatis-Plus 提供的 list() 方法查询所有评论
        return productReviewMapper.selectList(null);
    }
}
